package com.example.router

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.example.base.RoutePath
import com.tencent.mmkv.MMKV

/**
 * token拦截器
 */
@Interceptor(priority = 4, name = "路由Token"  )
class RouteTokenInterceptor :IInterceptor{
    private lateinit var mContext: Context

    private val path = arrayListOf("")//RoutePath.ROUTE_DETAIL
    override fun init(context: Context?) {
        if(context!=null){
            mContext = context
        }
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        if(path.contains(postcard.path)){
            var token = MMKV.defaultMMKV().decodeString("token","")
            if(token.isNullOrEmpty()){
                postcard.group = RoutePath.GROUP_LOGIN
                postcard.path = RoutePath.ROUTE_LOGIN
                postcard.navigation()
            }else{
                callback.onContinue(postcard)
            }
        }else{
            callback.onContinue(postcard)
        }
    }
}